package com.ynny.dao;

import com.ynny.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDaoImpl implements StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addStudent(Student student) {
        String sql = "INSERT INTO student (number, name, grade, clbum, gender) VALUES (?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, student.getNumber(), student.getName(), student.getGrade(), student.getClbum(), student.getGender());
    }

    @Override
    public void updateUserByName(String name, Student updatedStudent) {
        String sql = "UPDATE student SET number = ?, grade = ?, clbum = ?, gender = ? WHERE name = ?";
        jdbcTemplate.update(sql, updatedStudent.getNumber(), updatedStudent.getGrade(), updatedStudent.getClbum(), updatedStudent.getGender(), name);
    }

    @Override
    public Student getStudentByNumber(String number) {
        String sql = "SELECT * FROM student WHERE number = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{number}, new BeanPropertyRowMapper<>(Student.class));
    }

    @Override
    public List<Student> getAllStudents() {
        String sql = "SELECT * FROM student";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
    }
}
